/*  -------------------------- (C) COPYRIGHT 2022 Fortiortech ShenZhen ---------------------------*/
/**
 * @copyright (C) COPYRIGHT 2022 Fortiortech Shenzhen
 * @file      xxx.c
 * @author    Fortiortech  Appliction Team
 * @since     Create:2022-07-14
 * @date      Last modify:2022-07-14
 * @note      Last modify author is Leo.li
 * @brief
 */

/* Includes -------------------------------------------------------------------------------------*/
#include <FU68xx_2.h>
#include <Myproject.h>

uint8 SW1_Flag = 0;
uint8 SW2_Flag = 0;
uint8 SW3_Flag = 0;

uint8 Flag_Key1Value = 0; // 档位变换顺序
uint8 Flag_Key2Value = 0;

/* Private variables ----------------------------------------------------------------------------*/
KeyScanParam_TypeDef xdata KS;

/*  -------------------------------------------------------------------------------------------------
    Function Name : void KeyInit(void)
    Description   : 按键参数初始化
    Input         : 无
    Output                :   无
    -------------------------------------------------------------------------------------------------*/
void KeyInit(void)
{
    uint8 i = 0;
    uint8 Check_code = 0;      // 写入数值为Write_Lenght，最后一位为校验位0xFF
    uint16 ReadInfor_Addr = 0; // 读取信息地址
    KS.Key1Value = 0x01;
    // KS.Key2Value = 0x20;
    // KS.Key3Value = 0x00;
    User.HeatFlag = 0;
    KS.KeyValuetotal = KS.Key1Value ^ KS.Key2Value ^ KS.Key3Value;
    /*----- 获取写入首地址 -----*/
    FlashData.WriteAddress = GetWrite_Black_Addr(START_WriteADDRESS, Write_Lenght);

    if (FlashData.WriteAddress > START_WriteADDRESS)
    {
        /*----- 获取读取数值首地址 -----*/
        FlashData.ReadAddress = FlashData.WriteAddress - Write_Lenght;

        while ((FlashData.WriteAddress > START_WriteADDRESS) && (Check_code != Verify_Ture))
        {
            ReadInfor_Addr = FlashData.ReadAddress + Verify_Bit;
            Check_code = *(uint8 code *)(ReadInfor_Addr);

            if (Check_code == Verify_Ture)
            {
                /*----- 读取数值到FlashData.ReadValue -----*/
                ReadFromFlash(FlashData.ReadAddress, Write_Lenght, FlashData.ReadValue);
                KS.KeyValuetotal = FlashData.ReadValue[0]; // 有数值直接读回上次保存的数值
            }
            else
            {
                FlashData.ReadAddress = FlashData.ReadAddress - Write_Lenght;
            }
        }
    }
    else
    {
        /*----- 获取读取数值首地址 -----*/
        FlashData.ReadAddress = FlashData.WriteAddress;
    }

    /*Flash写一个扇区 2/3 区域后开始尝试擦写*/
    if (FlashData.WriteAddress >= START_WriteADDRESS + 0x060) //
    {
        for (i = 0; i < Write_Lenght; i++)
        {
            FlashData.WriteValue[i] = FlashData.ReadValue[i];
        }
        FlashData.Flag_FlashErase = 1;
    }

    KS.Key1Value = KS.KeyValuetotal & 0x0F;
    // KS.Key2Value = KS.KeyValuetotal & 0x70;
    KS.Key2Value = 0x20; 
    KS.Key3Value = KS.KeyValuetotal & 0x80; // 冷热风循环也可以记忆
    KS.KeyValuetotalInit = KS.Key1Value ^ KS.Key3Value;
    // KS.Key3Value = 0x00; // 初始化的时候取消了冷热风循环
    KS.OldKeyValuetotal = 0;
    KS.ChangeKeyFlg = 0;
    KS.Key1PressCnt = 0;
}

/*  -------------------------------------------------------------------------------------------------
    Function Name : uint8 KeyValue(void)
    Description   : 功能函数，获取按键值，选择返回键位值还是管脚电平KeyValue
    Input         : 无
    Output                :   键位值或者管脚电平KeyValue
    -------------------------------------------------------------------------------------------------*/
int KeyValue(void)
{
    /* SW1 Scan */
    if (SW1 == 0) // 热量按键
    {

        if (KS.Key1PressCnt < KeyFilterTime)
        {
            KS.Key1PressCnt++;
        }
        else if (SW1_Flag == 0)
        {
            SW1_Flag = 1;
        }
        else if (SW1_Flag == 1)
        {
            if (KS.Key1PressCnt < 80 * KeyFilterTime)
            {
                KS.Key1PressCnt++;
            }
            else
            {
                SW1_Flag = 2;

                if (KS.Key3Value == 0x80)
                {
                    KS.Key3Value = 0x00;
                    User.HeatFlag = 0;
                }
                else
                {
                    KS.Key3Value = 0x80;
                    if (KS.Key1Value == 0x01)
                    {
                        ledCt1.timeCnt = 6000;
                    }
                }
            }
        }
    }
    else
    {
        if (SW1_Flag == 1) // 短按功能实现
        {
            SW1_Flag = 3;
            if (KS.Key3Value == 0x80)
            {
                KS.Key3Value = 0x00;
                User.HeatFlag = 0;
            }
            else
            {
                if (KS.Key1Value == 0x01) // 往高档调
                {
                    KS.Key1Value = 0x02;
                }
                else if (KS.Key1Value == 0x02)
                {
                    KS.Key1Value = 0x04;
                }
                else
                {
                    KS.Key1Value = 0x01;
                }
            }
        }

        if (KS.Key1PressCnt > 0)
        {
            KS.Key1PressCnt--;
        }
        else
        {
            SW1_Flag = 0;
        }
    }
    AD_KeyScan();
    return (KS.Key1Value ^ KS.Key2Value ^ KS.Key3Value);
}

/*  -------------------------------------------------------------------------------------------------
    Function Name : void KeyScan(void)
    Description   : 功能函数，按键扫描，按键触发传递出按键命令
    Input         : 无
    Output                :   无
    -------------------------------------------------------------------------------------------------*/
void KeyScan(void)
{
    KS.OldKeyValuetotal = KS.KeyValuetotal;
    KS.KeyValuetotal = KeyValue();

    if (KS.OldKeyValuetotal != KS.KeyValuetotal)
    {
        if (mcFocCtrl.ZeroLossFlag == 0)
        {
            KS.ChangeKeyFlg = 1;
        }
    }
}

void AD_KeyScan(void) // 开关挡位扫描
{
    static uint8 TimeCnt = 0;
    static uint8 Key_Flag1 = 0;
    static uint8 Key_Flag2 = 0;
    static uint8 Key_Times1 = 0;
    static uint8 Key_Times2 = 0;

    KS.Key_Bus1 = ADC7_DR << 3;

    TimeCnt++;

    if (TimeCnt < 100)
    {
        if (KS.Key_Bus1 > 2000)
            Key_Times1++;
    }
    else
    {
        TimeCnt = 0;
        if (Key_Times1 >= 30)
        {
            Key_Flag1 = 1;
        }
        else if (Key_Times1 <= 1)
        {
            Key_Flag1 = 0;
        }

        if (Key_Flag1 == 1)
        {
            if (KS.Key2Value != 0x40)
            {
                KS.Key2Value = 0x40; // 高档转速
                KS.ChangeKeyFlg = 1;
            }
        }
        else

        {
            if (KS.Key2Value != 0x20)
            {
                KS.Key2Value = 0x20; // 低档转速
                KS.ChangeKeyFlg = 1;
            }
        }
        Key_Times1 = 0;
    }
}
